//
// Created by liushuai on 2021/1/28.
//

#include "numIslands.h"

void dfsIslands(vector<vector<char>> &grids, int row, int col) {
    int rows = grids.size();
    int cols = grids[0].size();
    grids[row][col] = '0';
    if (row - 1 >= 0 && grids[row - 1][col] == '1')//上
        dfsIslands(grids, row - 1, col);
    if (row + 1 < rows && grids[row + 1][col] == '1')//下
        dfsIslands(grids, row + 1, col);
    if (col - 1 >= 0 && grids[row][col - 1] == '1')//左
        dfsIslands(grids, row, col - 1);
    if (col + 1 < cols && grids[row][col + 1] == '1')//右
        dfsIslands(grids, row, col + 1);
}

int numIslands(vector<vector<char> > &grids) {
    if (grids.empty())
        return 0;
    int rows = grids.size();
    int cols = grids[0].size();
    int nums = 0;
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            if (grids[r][c] == '1') {
                nums += 1;
                dfsIslands(grids, r, c);
            }

        }
    }
    return nums;

}